home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / games1 / joystick.bas < prev    next >
BASIC Source File  |  1995-09-08  |  4KB  |  125 lines

  1. Attribute VB_Name = "JOYSTICK"
  2. Option Explicit
  3. '-------------------------------------------------------
  4. ' JOYSTICK.BAS - Joystick support routines for
  5. '                Visual Basic.
  6. '-------------------------------------------------------
  7.  
  8. ' Joystick Device ID
  9. Global Const JOYSTICK1 = 0
  10. Global Const JOYSTICK2 = 1
  11.  
  12. ' Joystick error return values
  13. Global Const JOYERR_NOERROR = 0
  14. Global Const JOYERR_PARMS = 165
  15. Global Const MMSYSERR_NODRIVER = 6
  16. Global Const JOYERR_UNPLUGGED = 167
  17.  
  18. ' Joystick button bit-flags used by tJoyInfo.ButtonStates
  19. Global Const JOY_BUTTON1 = &H1
  20. Global Const JOY_BUTTON2 = &H2
  21. Global Const JOY_BUTTON3 = &H4
  22. Global Const JOY_BUTTON4 = &H8
  23.  
  24.  
  25. ' Joystick Position
  26. Type tJoyInfo
  27.     Xin As Integer
  28.     Yin As Integer
  29.     Zin As Integer
  30.     ButtonStates As Integer
  31.     
  32.     ' These values are determined by the fields above.
  33.     X As Long
  34.     Y As Long
  35.     Z As Long
  36.     ButtonDown(1 To 4) As Integer
  37. End Type
  38.  
  39. ' Joystick Capabilities
  40.  
  41. Const MAXPNAMELEN = 32
  42.  
  43. Type tJoyCaps
  44.     Mid As Integer
  45.     Pid As Integer
  46.     Pname As String * MAXPNAMELEN
  47.     XminIn As Integer
  48.     XmaxIn As Integer
  49.     YminIn As Integer
  50.     YmaxIn As Integer
  51.     ZminIn As Integer
  52.     ZmaxIn As Integer
  53.     NumButtons As Integer
  54.     PeriodMin As Integer
  55.     PeriodMax As Integer
  56.  
  57.     Xmin As Long
  58.     Xmax As Long
  59.     Ymin As Long
  60.     Ymax As Long
  61.     Zmin As Long
  62.     Zmax As Long
  63. End Type
  64.  
  65. Global JoyCaps As tJoyCaps
  66.  
  67. ' Joystick API Calls
  68. Declare Function joyGetDevCaps Lib "winmm.dll" Alias "joyGetDevCapsA" (ByVal id As Long, lpCaps As tJoyCaps, ByVal uSize As Long) As Long
  69. Declare Function joyGetPos Lib "winmm.dll" (ByVal uJoyID As Long, pji As tJoyInfo) As Long
  70.  
  71.  
  72. Function GetJoyStickPos(IDDevice As Integer, JoyInfo As tJoyInfo) As Integer
  73. '-------------------------------------------------------
  74. ' This function is a wrapper around the joyGetPos API
  75. ' call.  That call returns coordinates as unsigned
  76. ' long integers, which VB doesn't support.  We move
  77. ' these coordinates into long values so that they
  78. ' can be easily evaluated.
  79. '-------------------------------------------------------
  80. Dim rc As Integer
  81. Static NotFirstTime As Integer
  82.  
  83.     If Not NotFirstTime Then
  84.         NotFirstTime = False
  85.         rc = joyGetDevCaps(IDDevice, JoyCaps, Len(JoyCaps))
  86.  
  87.         If rc <> 0 Then
  88.             GetJoyStickPos = rc
  89.             Exit Function
  90.         End If
  91.  
  92.         JoyCaps.Xmax = uint_to_long(JoyCaps.XmaxIn)
  93.         JoyCaps.Xmin = uint_to_long(JoyCaps.XminIn)
  94.         JoyCaps.Ymax = uint_to_long(JoyCaps.YmaxIn)
  95.         JoyCaps.Ymin = uint_to_long(JoyCaps.YminIn)
  96.         JoyCaps.Zmax = uint_to_long(JoyCaps.ZmaxIn)
  97.         JoyCaps.Zmin = uint_to_long(JoyCaps.ZminIn)
  98.  
  99.     End If
  100.  
  101.     rc = joyGetPos(IDDevice, JoyInfo)
  102.     GetJoyStickPos = rc
  103.  
  104.     If rc <> 0 Then Exit Function
  105.  
  106.     JoyInfo.X = uint_to_long(JoyInfo.Xin)
  107.     JoyInfo.Y = uint_to_long(JoyInfo.Yin)
  108.     JoyInfo.Z = uint_to_long(JoyInfo.Zin)
  109.  
  110.     JoyInfo.ButtonDown(1) = (JoyInfo.ButtonStates And JOY_BUTTON1) = JOY_BUTTON1
  111.     JoyInfo.ButtonDown(2) = (JoyInfo.ButtonStates And JOY_BUTTON2) = JOY_BUTTON2
  112.     JoyInfo.ButtonDown(3) = (JoyInfo.ButtonStates And JOY_BUTTON3) = JOY_BUTTON3
  113.     JoyInfo.ButtonDown(4) = (JoyInfo.ButtonStates And JOY_BUTTON4) = JOY_BUTTON4
  114.  
  115. End Function
  116.  
  117. Function uint_to_long(uint As Integer) As Long
  118. '-------------------------------------------------------
  119. ' Convert and unsigned integer into a long integer.
  120. '-------------------------------------------------------
  121.     
  122.     uint_to_long = (CLng(uint) And &HFFFF&)
  123. End Function
  124.  
  125.